package com.blank;

import com.blank.pojo.bo.User;
import com.blank.service.CheckLoginService;
import com.blank.service.Impl.CheckLoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author BLANK
 * @date 2020/11/12
 */
@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private CheckLoginService checkLoginService = new CheckLoginServiceImpl();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username=request.getParameter("username");
    String password =request.getParameter("password");

        try {
            User user = checkLoginService.checkLogin(username,password);
            if (user!=null){
                HttpSession session =request.getSession();
                session.setAttribute("user",user);
                response.sendRedirect("ViewIndexServlet");
            }
            else {
                response.sendRedirect("login");
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }

    }
}
